<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>数据演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f8f8f8;
            text-align: center;
            margin-top: 60px;
        }
        .progress-bar {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }
        .block {
            width: 40px;
            height: 40px;
            margin: 0 8px;
            border: 2px solid #006644;
            border-radius: 6px;
            box-sizing: border-box;
            transition: background 0.2s;
        }
        .block.on {
            background: #a8e6a3;
        }
        .block.off {
            background: #111;
        }
        .block {
            box-shadow: 0 2px 6px rgba(0,0,0,0.07);
        }
        .time {
            font-size: 1.2em;
            color: #333;
            margin-bottom: 16px;
        }
        .error {
            color: #fff;
            background: #d32f2f;
            padding: 10px 20px;
            border-radius: 6px;
            display: inline-block;
            margin-bottom: 16px;
        }
    </style>
    <script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
</head>
<body>
    <h2>数据演示</h2>
    <div id="error" class="error" style="display:none;"></div>
    <div class="progress-bar" id="progress-bar">
        
    </div>
    <div class="time" id="time">等待数据...</div>

    <script>
        const socket = io('/multitask');
        const progressBar = document.getElementById('progress-bar');
        const timeDiv = document.getElementById('time');
        const errorDiv = document.getElementById('error');

        socket.on('connect', function() {
            console.log('Socket.IO 连接已建立');
        });
        socket.on('disconnect', function() {
            errorDiv.style.display = 'block';
            errorDiv.textContent = 'Socket.IO 连接已关闭';
        });
        socket.on('connect_error', function() {
            errorDiv.style.display = 'block';
            errorDiv.textContent = 'Socket.IO 连接错误';
        });
        socket.on('message', function(data) {
            if (data.type == 'time_series') {
                console.log('message', data);
                // 错误处理
                if (data.index === -1) {
                    errorDiv.style.display = 'block';
                    errorDiv.textContent = data.errMsg || '未知错误';
                    showJson(-1);
                    timeDiv.textContent = '';
                    return;
                } else {
                    errorDiv.style.display = 'none';
                }
                showJson(data);
                timeDiv.textContent = data.time || '';
            }
        });

        function showJson(data) {
            document.getElementById("progress-bar").innerText = JSON.stringify(data.data);
        }
    </script>
</body>
</html> 